/*
 * Main.java
 * 
 * This file is part of Netuno.
 * 
 * CajuScript is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3, or (at your option) 
 * any later version.
 * 
 * CajuScript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with CajuScript.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.netuno;

import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.net.*;
import java.io.*;

/**
 * Start Netuno Server.
 * @author eduveks
 */
public final class Main {
    /**
     * Logger.
     */
    private static Logger logger = Logger.getLogger(Main.class);
    /**
     * Main.
     */
    private Main() { }
        
    /**
     * Start Netuno Server.
     * @param args Arguments
     */
    public static void main(final String[] args) {
        PropertyConfigurator.configure("log.properties");
        Properties config = new Properties();
        for (int x = 0; x < args.length; x++) {
            if (args[x].indexOf("=") != -1) {
                String[] argsSplit = args[x].split("=");
                if (argsSplit.length == 2) {
                    config.setProperty(argsSplit[0], argsSplit[1]);
                }
            } else {
                config.setProperty(args[x], args[x]);
            }
        }
        boolean quiet = false;
        try {
            if (config.getProperty("quiet").equals("quiet")) {
                quiet = true;
            }
        } catch (Exception e) {
            System.out.println();
            System.out.println("Netuno 0.1");
            System.out.println("http://www.netuno.org");
            System.out.println("Copyright(c)");
            System.out.println("All Rights Reserved.");
            System.out.println("GNU General Public License 3.0");
            System.out.println("http://www.gnu.org/licenses/gpl.html");
            System.out.println();
            System.out.println();
            logger.info("Netuno start.");
        }
        try {
            Config.load();
            Config.loadFile();
            logger.info("Config loaded.");
            Config.setServerSocket(new ServerSocket(Config.getPort()));
            logger.info("Server socket ready.");
            if (!quiet) {
                System.out.println("STARTED - "
                + Config.getServerSocket().getLocalSocketAddress());
                System.out.println();
            }
            ServiceMonitor serviceMonitor = new ServiceMonitor();
            serviceMonitor.go();
            Jobs jobs = new Jobs();
            jobs.start();
        } catch (Exception e) {
            logger.error("Server socket error.", e);
            System.out.println("ERROR: ");
            e.printStackTrace();
        }
    }
    /**
     * Get all jars in a folder and build the class path.
     * @param path Folder.
     * @return Class path with all jars.
     */
    public static String getLibsToClassPath(String path) {
        String allLib = "";
        File[] files = new File(path).listFiles();
        for (int x = 0; x < files.length; x++) {
            if (files[x].isDirectory()) {
                String subdir = getLibsToClassPath(files[x].toString());
                allLib += subdir.equals("") ? subdir : System.getProperty("path.separator") + subdir;
            } else {
                if (files[x].toString().toLowerCase().endsWith(".jar")) {
                    allLib += System.getProperty("path.separator");
                    allLib += files[x].toString();
                }
            }
        }
        return allLib.equals("") ? allLib : allLib.substring(1);
    }
}
